//
//  LYImageContentView.m
//  artist
//
//  Created by Jay_Apple on 16/4/26.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYImageContentView.h"
#import "LYPostDetailModel.h"
#import "SDPhotoBrowser.h"

@interface LYImageContentView ()<SDPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *images;

@end

@implementation LYImageContentView


- (void)setModel:(LYPostDetailModel *)model{
    
    
    _model = model;
    
    self.images = _model.images;
}

- (void)setImages:(NSArray *)images{
    
    _images = images;
    
    for (UIImageView *imageView in self.subviews) {
        if ([imageView isKindOfClass:[UIImageView class]]) {
            [imageView removeFromSuperview];
        }
        
    }
    
    for (NSInteger i = 0; i < images.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        
        //手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkPhoto:)];
        [imageView addGestureRecognizer:tap];
        
        imageView.tag = i;
        [self addSubview:imageView];
        
    }
    
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    NSUInteger row = 0;   //行
    NSUInteger colum = 0; //列
    CGFloat X;
    CGFloat Y;
    CGFloat W;
    CGFloat H;
    
    
    for (UIImageView *imageView in self.subviews) {
        
        if ([imageView isKindOfClass:[UIImageView class]]) {
            
            //单图
            if (self.images.count == 1) {
                
                X = 0;
                Y = 0;
                W = self.My_Width;
                H = self.My_Height;
                
                imageView.frame = CGRectMake(X, Y, W, H);
                
                
                if (self.model.downloadImage) {
                    imageView.image = self.model.downloadImage;
                    return;
                }
                __weak typeof(self) weakSelf = self;
                __weak typeof(imageView) weakImageView = imageView;
                [imageView sd_setImageWithURL:[NSURL URLWithString:[self.images firstObject]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                    if (!error) {
                        weakImageView.image = image;
                        weakSelf.model.downloadImage = image;
                        
                        [[NSNotificationCenter defaultCenter] postNotificationName:LYImageDownloadFinished object:nil];
                    }
                    
                }];
                
            }else if(self.images.count == 4){//4图
                
                W = LYFriendImageGroupHW;
                H = LYFriendImageGroupHW;
                
                //设置对应btn Frame
                row = imageView.tag / 2;
                colum = imageView.tag % 2;
                X = colum * (W + LYFriendImageMargin);
                Y =  row * (H + LYFriendImageMargin);
                imageView.frame = CGRectMake(X, Y, W, H);
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:self.images[imageView.tag]]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                    if (!error) {

                        
                        imageView.image = image;
                        
                    }
                    
                }];
                
                
                
            }else{//多图
                
                W = LYFriendImageGroupHW;
                H = LYFriendImageGroupHW;
                
                
                
                //设置对应btn Frame
                row = imageView.tag / LYFriendImageColum;
                colum = imageView.tag % LYFriendImageColum;
                X = colum * (W + LYFriendImageMargin);
                Y =  row * (H + LYFriendImageMargin);
                imageView.frame = CGRectMake(X, Y, W, H);
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:self.images[imageView.tag]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                    if (!error) {
                        
                        
                        imageView.image = image;
                        
                    }
                    
                    
                }];
                
                
            }
            
            
        }
    }
}


#pragma mark - 查看图片
- (void)checkPhoto:(UITapGestureRecognizer *)tap{

    UIImageView *imageV = (UIImageView *)tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self; // 原图的父控件
    browser.imageCount = self.images.count; // 图片总数
    browser.currentImageIndex = imageV.tag;
    browser.delegate = self;
    [browser show];


}

#pragma mark - browserDelegate

#pragma mark - photobrowser代理方法

// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return [self.subviews[index] image];
}


// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = self.images[index];
    return [NSURL URLWithString:urlStr];
}


@end
